<template>
  <div class="container-whi-group">
    <el-dialog
      :title="dialogConfig.name"
      :visible.sync="dialogVisible"
      @close="dialogVisible = false"
      append-to-body
    >
      <el-form ref="formRef" :model="formData" :rules="rules">
        <el-form-item label="组id" :label-width="'100px'" prop="uid">
          <!-- <el-input v-model="formData.groupId" autocomplete="off"></el-input> -->
          <el-select v-model="formData.groupId" placeholder="请选择">
            <el-option
              v-for="item in groupIdOptions"
              :key="item.groupId"
              :label="item.groupName"
              :value="item.groupId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目标id" :label-width="'100px'" prop="name">
          <el-input v-model="formData.targetId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="'100px'" prop="link">
          <el-input v-model="formData.remark" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 先传到控制台服务器 然后后端再调s3的api -->
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>

      <!-- <el-dialog :visible.sync="imageDialogVis" append-to-body>
        <el-image :fit="'contain'" :src="imageToView"></el-image>
      </el-dialog> -->
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { groupAddApi, groupUpdateApi } from '@api/whisper'
export default {
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    value: {
      type: Object
    },
    request: {
      type: Object
    },
    dialogConfig: {
      type: Object
    }
  },
  data() {
    return {
      posting: false,
      formData: this.setup(this.value),
      rules: {},
      // imageToView: null,
      // imageDialogVis: false
      groupIdOptions: [
        {
          groupName: 'uid审核组',
          groupId: '0'
        },
        {
          groupName: 'IP审核组',
          groupId: '1'
        },
        {
          groupName: '设备审核组',
          groupId: '2'
        },
        {
          groupName: '邮箱免罚发邮件组',
          groupId: '3'
        }
        // {
        //   label: '推荐白名单',
        //   value: 4
        // },
        // {
        //   label: '系统用户组',
        //   value: 5
        // }
      ]
    }
  },
  created() {},
  mounted() {},
  methods: {
    setup() {
      if (!this.value) {
        // this.groupIdItem()
        return {
          groupId: '',
          targetId: '',
          operator: ''
        }
      } else {
        const clone = deepClone(this.value)
        console.log('----', clone)
        return clone
      }
    },

    // groupIdItem() {
    //   groupIdItemApi().then(res => {
    //     console.log(res)
    //     if (res) {
    //       this.groupIdOptions = res.data
    //     }
    //   })
    // },

    handleConfirm() {
      this.posting = true
      let func = null
      if (this.formData.id) {
        func = groupUpdateApi
      } else {
        func = groupAddApi
      }
      const clone = deepClone(this.formData)
      func(clone)
        .then(res => {
          this.dialogVisible = false
          this.$emit('success') // 刷新
        })
        .catch(err => {
          this.$message('error')
        })
    }

    // 上传图片
  },
  watch: {
    visible(val) {
      if (val) {
        this.formData = this.setup()
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.container-whi-group {
}
</style>
